Skip to content

Make bounded-queue object a struct#3

Open
sdilts wants to merge 1 commit intoItsMeForLua:mainfrom
sdilts:bounded-queue-struct
Open

Make bounded-queue object a struct#3
sdilts wants to merge 1 commit intoItsMeForLua:mainfrom
sdilts:bounded-queue-struct

Conversation

@sdilts
Copy link
Copy Markdown

@sdilts sdilts commented Mar 17, 2026

This allows SBCL to derive the type of the object's slots, which removes several warnings about dynamic dispatching of aref calls.

  • Mark capacity slot as a fixnum, as that's what it is being treated as throughout the code; although the right thing appeared to be occuring, the usage of the the operator conflicted with its (unsigned-byte 32) type, which is undefined behavior. At least on SBCL, it's now possible to remove the related the calls and get the same behavior.
  • Hopefully do the same thing with the value of numbers in the
    sequences array.

I looked at doing this for the SPSC queue, but I don't know how it would affect the cache thing that's going on.

@sdilts
Copy link
Copy Markdown
Author

sdilts commented Mar 17, 2026

Here's out the compiler output changed:

Warnings Before

; compiling file "/home/sdilts/Programs/mahogany/dependencies/cl-freelock/src/bounded-queue.lisp" (written 17 MAR 2026 12:37:43 PM):

; file: /home/sdilts/Programs/mahogany/dependencies/cl-freelock/src/bounded-queue.lisp
; in: DEFUN BOUNDED-QUEUE-PUSH
; (AREF (CL-FREELOCK::BOUNDED-QUEUE-SEQUENCES CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; (AREF (CL-FREELOCK::BOUNDED-QUEUE-BUFFER CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
; --> FUNCALL
; ==>
; 1
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; in: DEFUN BOUNDED-QUEUE-POP
; (AREF (CL-FREELOCK::BOUNDED-QUEUE-SEQUENCES CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; (AREF (CL-FREELOCK::BOUNDED-QUEUE-BUFFER CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; in: DEFUN BOUNDED-QUEUE-PUSH-BATCH
; (LENGTH SEQUENCE)
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a T, not a VECTOR.

; (AREF (CL-FREELOCK::BOUNDED-QUEUE-SEQUENCES CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; (ELT SEQUENCE CL-FREELOCK::I)
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a (OR CONS VECTOR
; SB-KERNEL:EXTENDED-SEQUENCE), not a SIMPLE-ARRAY.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a (OR CONS VECTOR
; SB-KERNEL:EXTENDED-SEQUENCE), not a LIST.

; (AREF (CL-FREELOCK::BOUNDED-QUEUE-BUFFER CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
; --> FUNCALL
; ==>
; 1
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; (+ (- CL-FREELOCK::TAIL CL-FREELOCK::HEAD) CL-FREELOCK::BATCH-SIZE)
;
; note: forced to do +/SIGNED=>INTEGER (cost 7)
; unable to do inline fixnum arithmetic (cost 2) because:
; The first argument is a (INTEGER -9223372036854775807 9223372036854775807), not a FIXNUM.
; The result is a (VALUES
; (INTEGER -9223372036854775807 9223389629040820222)
; &OPTIONAL), not a (VALUES FIXNUM &OPTIONAL).

; (> (+ (- CL-FREELOCK::TAIL CL-FREELOCK::HEAD) CL-FREELOCK::BATCH-SIZE)
; (CL-FREELOCK:BOUNDED-QUEUE-CAPACITY CL-FREELOCK:QUEUE))
;
; note: forced to do GENERIC-> (cost 10)
; unable to do inline fixnum comparison (cost 4) because:
; The first argument is a (INTEGER -9223372036854775807 9223389629040820222), not a FIXNUM.
; The second argument is a T, not a FIXNUM.

; (LOGAND (CL-FREELOCK::BOUNDED-QUEUE-MASK CL-FREELOCK:QUEUE)
; CL-FREELOCK::CURRENT-TAIL)
;
; note: forced to do full call
; unable to do inline fixnum arithmetic (cost 2) because:
; The first argument is a T, not a FIXNUM.
; The second argument is a (INTEGER -4611686018427387904
; 4611703610613432317), not a FIXNUM.
; The result is a (VALUES INTEGER &OPTIONAL), not a (VALUES FIXNUM
; &OPTIONAL).
; unable to do inline (unsigned-byte 64) arithmetic (cost 3) because:
; The first argument is a T, not a (UNSIGNED-BYTE 64).
; The second argument is a (INTEGER -4611686018427387904
; 4611703610613432317), not a (UNSIGNED-BYTE 64).
; The result is a (VALUES INTEGER &OPTIONAL), not a (VALUES
; (UNSIGNED-BYTE 64)
; &OPTIONAL).
; etc.

; (+ CL-FREELOCK::TAIL CL-FREELOCK::BATCH-SIZE)
;
; note: doing signed word to integer coercion (cost 20), for:
; the first result of inline (signed-byte 64) arithmetic

; (LOGAND (CL-FREELOCK::BOUNDED-QUEUE-MASK CL-FREELOCK:QUEUE)
; CL-FREELOCK::CURRENT-TAIL)
;
; note: doing signed word to integer coercion (cost 20)

; in: DEFUN BOUNDED-QUEUE-POP-BATCH
; (AREF (CL-FREELOCK::BOUNDED-QUEUE-SEQUENCES CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; (AREF (CL-FREELOCK::BOUNDED-QUEUE-BUFFER CL-FREELOCK:QUEUE)
; CL-FREELOCK::INDEX)
;
; note: unable to
; avoid runtime dispatch on array element type
; because:
; Upgraded element type of array is not known at compile time.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a VECTOR, not a SIMPLE-ARRAY.

; (+ CL-FREELOCK::HEAD CL-FREELOCK::BATCH-SIZE)
;
; note: forced to do +/SIGNED=>INTEGER (cost 7)
; unable to do inline fixnum arithmetic (cost 2) because:
; The second argument is a (OR (INTEGER -9223372036854775807 -1)
; (INTEGER 1 17592186044415)), not a FIXNUM.
; The result is a (VALUES
; (INTEGER -13835058055282163711 4611703610613432318)
; &OPTIONAL), not a (VALUES FIXNUM &OPTIONAL).

; (LOGAND (CL-FREELOCK::BOUNDED-QUEUE-MASK CL-FREELOCK:QUEUE)
; CL-FREELOCK::CURRENT-HEAD)
;
; note: forced to do full call
; unable to do inline fixnum arithmetic (cost 2) because:
; The first argument is a T, not a FIXNUM.
; The second argument is a (INTEGER -4611686018427387904
; 4611703610613432317), not a FIXNUM.
; The result is a (VALUES INTEGER &OPTIONAL), not a (VALUES FIXNUM
; &OPTIONAL).
; unable to do inline (unsigned-byte 64) arithmetic (cost 3) because:
; The first argument is a T, not a (UNSIGNED-BYTE 64).
; The second argument is a (INTEGER -4611686018427387904
; 4611703610613432317), not a (UNSIGNED-BYTE 64).
; The result is a (VALUES INTEGER &OPTIONAL), not a (VALUES
; (UNSIGNED-BYTE 64)
; &OPTIONAL).
; etc.

; (+ CL-FREELOCK::CURRENT-HEAD
; (CL-FREELOCK:BOUNDED-QUEUE-CAPACITY CL-FREELOCK:QUEUE))
;
; note: forced to do GENERIC-+ (cost 10)
; unable to do inline fixnum arithmetic (cost 2) because:
; The first argument is a (INTEGER -4611686018427387904 4611703610613432317), not a FIXNUM.
; The second argument is a T, not a FIXNUM.
; The result is a (VALUES NUMBER &OPTIONAL), not a (VALUES FIXNUM &OPTIONAL).
; unable to do inline (unsigned-byte 64) arithmetic (cost 4) because:
; The first argument is a (INTEGER -4611686018427387904 4611703610613432317), not a (UNSIGNED-BYTE
; 64).
; The second argument is a T, not a (UNSIGNED-BYTE 64).
; The result is a (VALUES NUMBER &OPTIONAL), not a (VALUES
; (UNSIGNED-BYTE 64)
; &OPTIONAL).
; etc.

; (LOGAND (CL-FREELOCK::BOUNDED-QUEUE-MASK CL-FREELOCK:QUEUE)
; CL-FREELOCK::CURRENT-HEAD)
;
; note: doing signed word to integer coercion (cost 20)

; (+ CL-FREELOCK::CURRENT-HEAD
; (CL-FREELOCK:BOUNDED-QUEUE-CAPACITY CL-FREELOCK:QUEUE))
;
; note: doing signed word to integer coercion (cost 20), for:
; the first argument of GENERIC-+

Warnings after

; compiling file "/home/sdilts/Programs/mahogany/dependencies/cl-freelock/src/bounded-queue.lisp" (written 17 MAR 2026 12:40:18 PM):

; file: /home/sdilts/Programs/mahogany/dependencies/cl-freelock/src/bounded-queue.lisp
; in: DEFUN BOUNDED-QUEUE-PUSH-BATCH
; (LENGTH SEQUENCE)
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a T, not a VECTOR.

; (ELT SEQUENCE CL-FREELOCK::I)
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a (OR CONS VECTOR
; SB-KERNEL:EXTENDED-SEQUENCE), not a SIMPLE-ARRAY.
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a (OR CONS VECTOR
; SB-KERNEL:EXTENDED-SEQUENCE), not a LIST.

; (+ (- CL-FREELOCK::TAIL CL-FREELOCK::HEAD) CL-FREELOCK::BATCH-SIZE)
;
; note: forced to do +/SIGNED=>INTEGER (cost 7)
; unable to do inline fixnum arithmetic (cost 2) because:
; The first argument is a (INTEGER -9223372036854775807 9223372036854775807), not a FIXNUM.
; The result is a (VALUES
; (INTEGER -9223372036854775807 9223389629040820222)
; &OPTIONAL), not a (VALUES FIXNUM &OPTIONAL).

; (+ CL-FREELOCK::TAIL CL-FREELOCK::BATCH-SIZE)
;
; note: doing signed word to integer coercion (cost 20), for:
; the first result of inline (signed-byte 64) arithmetic

; in: DEFUN BOUNDED-QUEUE-POP-BATCH
; (+ CL-FREELOCK::HEAD CL-FREELOCK::BATCH-SIZE)
;
; note: forced to do +/SIGNED=>INTEGER (cost 7)
; unable to do inline fixnum arithmetic (cost 2) because:
; The second argument is a (OR (INTEGER -9223372036854775807 -1)
; (INTEGER 1 17592186044415)), not a FIXNUM.
; The result is a (VALUES
; (INTEGER -13835058055282163711 4611703610613432318)
; &OPTIONAL), not a (VALUES FIXNUM &OPTIONAL).

; (+ CL-FREELOCK::CURRENT-HEAD
; (CL-FREELOCK:BOUNDED-QUEUE-CAPACITY CL-FREELOCK:QUEUE))
;
; note: forced to do +/SIGNED=>INTEGER (cost 7)
; unable to do inline fixnum arithmetic (cost 2) because:
; The first argument is a (INTEGER -4611686018427387904 4611703610613432317), not a FIXNUM.
; The result is a (VALUES
; (INTEGER -9223372036854775808 9223389629040820220)
; &OPTIONAL), not a (VALUES FIXNUM &OPTIONAL).

; wrote /home/sdilts/Programs/mahogany/build/asdf-cache/sbcl-2.6.2-linux-x64-s/dependencies/cl-freelock/src/bounded-queue-tmpGHU3ALSV.fasl
; compilation finished in 0:00:00.014

This allows SBCL to derive the type of the object's slots, which
removes several warnings about dynamic dispatching of `aref` calls.
+ Mark capacity slot as a fixnum, as that's what it is being treated as
  throughout the code; although the right thing appeared to be
  occuring, the usage of the `the` operator conflicted with its
  `(unsigned-byte 32)` type, which is undefined behavior.
+ Hopefully do the same thing with the value of numbers in the
  `sequences` array.
@sdilts sdilts force-pushed the bounded-queue-struct branch from c31a815 to c0183b8 Compare March 17, 2026 18:55
@ItsMeForLua ItsMeForLua added the enhancement New feature or request label Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants